<?php

require_once dirname(__FILE__).'/../lib/partidoAdminGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/partidoAdminGeneratorHelper.class.php';

/**
 * partidoAdmin actions.
 *
 * @package    ehockey
 * @subpackage partidoAdmin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class partidoAdminActions extends autoPartidoAdminActions
{
	public function executePlanilla(sfWebRequest $request)
	{
		$this->partido = $this->getRoute()->getObject();
				
		$this->forward404Unless($this->partido->isImprimible($this->getUser()->getUsuario()));
				
		LogPlanilla::logImpresionPlanilla($this->getUser()->getUsuario(), $this->partido);
	}

	public function executeValidar(sfWebRequest $request)
	{
		$this->partido = $this->getRoute()->getObject();
		
		$this->forward404Unless($this->partido->isVerificable($this->getUser()->getUsuario()));
		
		$this->form = new NotificacionLightForm();
		$this->form->setPartido($this->partido->getId());
	}
	public function executeSubmit(sfWebRequest $request)
	{

		$this->form = new NotificacionLightForm();
		if ($request->isMethod("post")) {
			$this->form->autoBind();
			if ($this->form->isValid()) {
				$this->getUser()->setFlash('notice', "eeeeeeeee");
				$this->redirect('@homepage');
			}
			$this->getUser()->setFlash('notice', $this->form->getValue('mensaje'));

			$this->redirect('@homepage');
		}
	}

	//	public function executeSubmit(sfWebRequest $request)
	//	{
	//		$this->redirect('@homepage');
	//
	//	}

  	public function executeEdit(sfWebRequest $request) {
  		parent::executeEdit($request);
		$this->forward404Unless($this->partido->isCompletable($this->getUser()->getUsuario()));
		
  	}
 
	protected function processForm(sfWebRequest $request, sfForm $form){
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid())
		{
			$notice = $form->getObject()->isNew() ? 'La planilla fue creada satisfactoriamente.' : 'La planilla fue actualizada satisfactoriamente.';

			try {
				$partido = $form->save();
				
				if(!$form->isContinuar()) {
					LogPlanilla::logCargaCompletaPlanilla($this->getUser()->getUsuario(), $partido);
				}else{
					LogPlanilla::logCargaParcialPlanilla($this->getUser()->getUsuario(), $partido);
				}
				
			} catch (Doctrine_Validator_Exception $e) {

				$errorStack = $form->getObject()->getErrorStack();

				$message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
				foreach ($errorStack as $field => $errors) {
					$message .= "$field (" . implode(", ", $errors) . "), ";
				}
				$message = trim($message, ', ');

				$this->getUser()->setFlash('error', $message);
				return sfView::SUCCESS;
			}

			$this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $partido)));

			$this->getUser()->setFlash('notice', $notice);

			$this->redirect('@homepage');	//redireccion a la homepage
		}
		else
		{
			$this->getUser()->setFlash('error', 'La planilla no fue guardada debido a errores.', false);
		}
	}
}
